package day7;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Market implements java.io.Serializable{
    private String name;
    private String adress;
    Map<String, Member> card = new HashMap<>();
    Scanner in = new Scanner(System.in);

    public Market(String name, String adress) {
        this.name = name;
        this.adress = adress;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAdress() {
        return adress;
    }

    public void setAdress(String adress) {
        this.adress = adress;
    }

    //添加会员
    public boolean add(String cardNo, Member a) {
        if (card.containsKey(cardNo)) {
            System.out.println("会员卡已被使用！");
            return false;
        }
        card.put(cardNo, a);
        return true;
    }

    //积分兑换
    public boolean change(String cardNo) {
        while (true) {
            if (!card.containsKey(cardNo)) {
                System.out.println("您输入的卡号不正确");
            } else {
                while (true) {
                    System.out.println("1.100积分兑换抽纸一包；2.500积分兑换电吹风一把；3.1000积分兑换炒锅一个；4退出。");
                    System.out.println("请选择");
                    int chosen = in.nextInt();
                    if (chosen == 1) {
                        if ((card.get(cardNo).getPoint()) >= 100) {
                            System.out.println("兑换抽纸一包，兑换成功！");
                            card.get(cardNo).setPoint(card.get(cardNo).getPoint() - 100);
                            return true;
                        }
                        System.out.println("抱歉，积分不够。兑换失败！");
                        return false;
                    }
                    if (chosen == 2) {
                        if (card.get(cardNo).getPoint() >= 500) {
                            System.out.println("兑换电吹风一个，兑换成功！");
                            card.get(cardNo).setPoint(card.get(cardNo).getPoint() - 500);
                            return true;
                        }
                        System.out.println("抱歉，积分不够。兑换失败！");
                        return false;
                    }
                    if (chosen == 3) {
                        if (card.get(cardNo).getPoint() >= 1000) {
                            System.out.println("兑换炒锅一个，兑换成功！");
                            card.get(cardNo).setPoint(card.get(cardNo).getPoint() - 1000);
                            return true;
                        }
                        System.out.println("抱歉，积分不够。兑换失败！");
                        return false;
                    }
                    if (chosen == 4) {
                        break;
                    } else {
                        System.out.println("选择错误，请重新选择！");
                    }
                }
            }
            return true;
        }
    }

    //修改密码
    public boolean pswdchange(String cardNo, String pswd) {

        int count = 0;//记录密码输错次数
        if (card.containsKey(cardNo)) {
            while(true) {
                if ((card.get(cardNo).getPswd()).equals(pswd)) {
                    System.out.println("输入新密码");
                    String newpswd = in.next();
                    card.get(cardNo).setPswd(newpswd);
                    return true;
                }
                System.out.println("密码不正确，请重新输入");
                pswd = in.next();
                count++;
                System.out.println("密码输入错误，你还有" + (3 - count) + "次机会");
                if (count == 3) {
                    System.out.println("机会已用完,你无权修改密码");
                    return false;
                }
            }
        } else {
            System.out.println("您输入的卡号不存在！");
            return false;
        }
    }

    public int querypoint(String cardNo) {
        if (card.containsKey(cardNo)) {
            System.out.println("卡号为" + cardNo + "卡上积分：" + card.get(cardNo).getPoint());
        } else {
            System.out.println("卡号不存在");
            return 0;
        }
        return card.get(cardNo).getPoint();
    }

    public int addpoint(String cardNo, int money) {
        if (card.containsKey(cardNo)) {
            card.get(cardNo).setPoint(card.get(cardNo).getPoint() + (money / 10));
        } else {
            System.out.println("卡号不存在");
            return 0;
        }
        return card.get(cardNo).getPoint();
    }
    //创建会员
    public void makememberfile()throws Exception{
        try {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("E:\\作业\\member.txt"));
            Member a=new Member("hy001","老李","18919897855",1000,"abc123");
            Member b=new Member("hy002","老张","13989765466",800,"ab1234");
            Member c=new Member("hy003","老王","17705667878",200,"a12345");
            oos.writeObject(a);
            oos.writeObject(b);
            oos.writeObject(c);
            oos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    //判断有没有会员
    public void check() throws Exception{
        if(card==null){
            this.makememberfile();
            ObjectInputStream ois=new ObjectInputStream(new FileInputStream("E:\\作业\\member.txt"));
            Member m;
            if((m= (Member) ois.readObject())!=null){
                card.put(m.getCardNo(),m);
            }
            ois.close();
        }
    }
}


